import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class test2 {
	public static void main(String[] args) {
		int[] nums = {-1,0,1,2,-1,-4};
		new test2();
		List<List<Integer>> list = test2.threeSum(nums);
		System.out.println(list);
	}

	public static List<List<Integer>> threeSum(int[] nums) {
		Set<List<Integer>> set = new HashSet<List<Integer>>();
		if (nums.length == 0)
			return new ArrayList<>(set);
		Arrays.sort(nums);
		for (int i = 0; i < nums.length - 2; i++) {
			if (i > 0 && nums[i] == nums[i - 1])
				continue;
			int j = i + 1;
			int k = nums.length - 1;
			while (j < k) {
				int sum = nums[i] + nums[j] + nums[k];
				if (sum > 0) {
					k--;
				} else if (sum < 0) {
					j++;
				} else {
					set.add(Arrays.asList(nums[i], nums[j++], nums[k--]));
				}
			}
		}
		return new ArrayList<List<Integer>>(set);
	}
}
